package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author uuei
 * @since 2021-07-11
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public IPage<Teacher> selectPage(Long page, Long limit, TeacherQuery teacherQuery) {
        Page<Teacher> pageParam = new Page<>(page, limit);

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if (teacherQuery == null) {
            return baseMapper.selectPage(pageParam, queryWrapper);
        }

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String joinDateEnd = teacherQuery.getJoinDateEnd();
        String joinDateBegin = teacherQuery.getJoinDateBegin();

//        if(!StringUtils.isEmpty(name)){
//            //左%会使索引失效
//            queryWrapper.likeRight("name",name);
//        }
//        if (level != null){
//            queryWrapper.eq("level",level);
//        }
//        if (!StringUtils.isEmpty(joinDateBegin)){
//            queryWrapper.ge("join_date",joinDateBegin);
//        }
//        if (!StringUtils.isEmpty(joinDateEnd)){
//            queryWrapper.le("join_date",joinDateEnd);
//        }


        queryWrapper.likeRight(!StringUtils.isEmpty(name), "name", name);
        queryWrapper.eq(level != null, "level", level);
        queryWrapper.ge(!StringUtils.isEmpty(joinDateBegin), "join_date", joinDateBegin);
        queryWrapper.le(!StringUtils.isEmpty(joinDateEnd), "join_date", joinDateEnd);


        return baseMapper.selectPage(pageParam, queryWrapper);
    }
}
